package com.aricwang.easyprofile.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.aricwang.easyprofile.profile.Profile;

public class ProfileTable {
	// Database table
	public static final String TABLE_NAME = "profile";
	
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "name";
	
	public static final String COLUMN_VOLUME_CALL = "volume_call";

	public static final String COLUMN_VIBRATE = "vibrate";
	
	public static final String COLUMN_CELL = "cell";
	public static final String COLUMN_WIFI = "wifi";
	public static final String COLUMN_BLUETOOTH = "bluetooth";

	public static final String COLUMN_TIMEOUT = "timeout";

	public static final String[] ALL_COLUMNS = { 
		ProfileTable.COLUMN_ID,
		ProfileTable.COLUMN_NAME,
		ProfileTable.COLUMN_VOLUME_CALL,
		ProfileTable.COLUMN_VIBRATE,
		ProfileTable.COLUMN_CELL,
		ProfileTable.COLUMN_WIFI,
		ProfileTable.COLUMN_BLUETOOTH,
		ProfileTable.COLUMN_TIMEOUT
	};


	// Database creation SQL statement
	private static final String DATABASE_CREATE = "create table " 
			+ TABLE_NAME
			+ "(" 
			+ COLUMN_ID + " integer primary key autoincrement, " 
			+ COLUMN_NAME + " text not null, " 
			+ COLUMN_VOLUME_CALL + " integer default -1, "
			+ COLUMN_VIBRATE + " integer default 1, "
			+ COLUMN_CELL + " integer default 1, "
			+ COLUMN_WIFI + " integer default 1, "
			+ COLUMN_BLUETOOTH + " integer default 0, "
			+ COLUMN_TIMEOUT + " integer default 30 "
			+ ");";

	public static void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
		
		insertDefaults(database);
	}

	public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		Log.w(ProfileTable.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		
		onCreate(database);
	}
	
	private static void insertDefaults(SQLiteDatabase database) {
		// profile 'Default'
		insertRecord(database, "Default", 
				Profile.DEFAULT_VOLUME, 
				Profile.ON,				// vibrate
				Profile.ON, 
				Profile.OFF, 
				Profile.OFF, 
				Profile.DEFAULT_SCREEN_TIMEOUT 
				);
		
		// profile 'Work'
		insertRecord(database, "Work", 
				0, 						// zero volume
				Profile.ON,				// vibrate
				Profile.ON,				// cell 
				Profile.OFF, 			// wifi
				Profile.OFF, 			// bluetooth
				Profile.DEFAULT_SCREEN_TIMEOUT 	// screen timeout
				);
		
		// profile 'Home'
		insertRecord(database, "Home", 
				Profile.DEFAULT_VOLUME, 
				Profile.ON,				// vibrate
				Profile.ON,				// cell 
				Profile.ON, 			// wifi
				Profile.OFF, 			// bluetooth
				Profile.DEFAULT_SCREEN_TIMEOUT 	// screen timeout
				);
		
		// profile 'Charging'
		insertRecord(database, "Charging", 
				Profile.NOCHANGE, 
				Profile.NOCHANGE,			// vibrate
				Profile.NOCHANGE,			// cell 
				Profile.NOCHANGE, 		// wifi
				Profile.NOCHANGE, 		// bluetooth
				Profile.NOCHANGE 	// screen timeout
				);

		// profile 'Sleep'
		insertRecord(database, "Sleep", 
				Profile.DEFAULT_VOLUME, 
				Profile.OFF,			// vibrate
				Profile.FLIGHT,			// cell 
				Profile.FLIGHT, 		// wifi
				Profile.FLIGHT, 		// bluetooth
				Profile.DEFAULT_SCREEN_TIMEOUT 	// screen timeout
				);

		// profile 'Home WLAN Only'
		insertRecord(database, "Home WLAN Only", 
				Profile.DEFAULT_VOLUME, 
				Profile.ON,				// vibrate
				Profile.OFF,			// cell 
				Profile.ON, 			// wifi
				Profile.OFF, 			// bluetooth
				Profile.DEFAULT_SCREEN_TIMEOUT 	// screen timeout
				);

				
	}
	
	private static void insertRecord(SQLiteDatabase database, String name, 
			int volumeCall, int vibrate, 
			int cell, int wifi, int bluetooth, 
			int timeout) 
	{
		String sql = "insert into " + TABLE_NAME  
				+ " ( "
				+ COLUMN_NAME + ", " 
				+ COLUMN_VOLUME_CALL + ", "
				+ COLUMN_VIBRATE + ", "
				+ COLUMN_CELL + ", "
				+ COLUMN_WIFI + ", "
				+ COLUMN_BLUETOOTH + ", "
				+ COLUMN_TIMEOUT + " "
				+ ") values ( '"
				+ name + "', "
				+ volumeCall + ", "
				+ vibrate + ", "
				+ cell + ", "
				+ wifi + ", "
				+ bluetooth + ", "
				+ timeout 
				+ " );";
	
		database.execSQL(sql);
	}
}
